﻿using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Bank_database_system.业务部
{

    public partial class 保险箱租用c : UserControl
    {
        private string branch_id = "";
        private string staff_id = "";
        private string Card_ID = "";
        public event EventHandler ButtonBack;
        // 定义数据库连接字符串作为常量
        public const string connectionString =
            @"Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=47.96.39.153)(PORT=1521))
            (CONNECT_DATA=(SID=orcl)));User Id=system;Password=Tongji123456;";

        int page = 1;
        int totalRecordCount = 1;
        public 保险箱租用c()
        {
            InitializeComponent();
            totalRecordCount = 
                Safe_Box_Operations.GetSafeBoxRecordCount
                (Card_ID, dateTimePicker1.Text, dateTimePicker2.Text);
            page = 1;
            dataGridView1.RowHeadersVisible = false;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToResizeColumns = false;
            dataGridView1.AllowUserToResizeRows = false;
            dataGridView1.ReadOnly = true;
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }
        private int GetTotalRecordCount(string creditCardNumbers)
        {
            int count = 0;
            string query = @"
        SELECT COUNT(*)
        FROM SAFE_BOX_HIRE
        WHERE CREDIT_CARD_NUMBERS = :CREDIT_CARD_NUMBERS";

            try
            {
                using (OracleConnection connection = new OracleConnection(connectionString))
                {
                    connection.Open();

                    using (OracleCommand command = new OracleCommand(query, connection))
                    {
                        // 添加参数并设置值
                        command.Parameters.Add("CREDIT_CARD_NUMBERS", OracleDbType.Varchar2).Value = creditCardNumbers;

                        // 执行查询并获取结果
                        count = Convert.ToInt32(command.ExecuteScalar());
                    }
                }
            }
            catch (OracleException ex)
            {
                MessageBox.Show($"数据库操作失败：{ex.Message}", "数据库错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"发生错误：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return count;
        }

        public void LoadSafeBoxData(string creditCardNumbers)
        {
            DateTime BeginTime;
            DateTime EndTime;

            BeginTime = DateTime.Now.AddYears(-1).Date;
            EndTime = DateTime.Now.AddYears(1).Date;

            string beginTimeString = BeginTime.ToString("yyyy-MM-dd"); // 转换为只保留年月日的字符串
            string endTimeString = EndTime.ToString("yyyy-MM-dd");     // 转换为只保留年月日的字符串
            // 调用函数
            DataTable safeBoxData = Safe_Box_Operations.
                GetSafeBoxRecords(page.ToString(), beginTimeString, endTimeString, creditCardNumbers);
            totalRecordCount = Safe_Box_Operations.
                GetSafeBoxRecordCount(Card_ID, beginTimeString, endTimeString);
            // 将返回的 DataTable 绑定到 dataGridView1
            dataGridView1.DataSource = safeBoxData;
            label9.Text = "当前第  " + $"{page}" + "  页        共有  " + $"{(totalRecordCount - 1) / 10 + 1}  页 ";
            SetDataGridViewColumnWidthsAndRowHeights();
        }

        private void SetDataGridViewColumnWidthsAndRowHeights()
        {
            DateTime today = DateTime.Today;
            dateTimePicker1.Value = today.AddDays(-365);
            dateTimePicker2.Value = today.AddDays(+365);
            if (dataGridView1.Rows.Count == 0)
            {
                // 如果 dataGridView1 没有任何行数据，则将 DataSource 设为 null
                dataGridView1.DataSource = null;
            }
            else
            {
                // 设置指定列的标题文本
                dataGridView1.Columns["SAFE_BOX_ID"].HeaderText = "保险箱ID";
                dataGridView1.Columns["HIRE_ID"].HeaderText = "租用ID";
                dataGridView1.Columns["HIRE_ID"].DisplayIndex = 0;
                dataGridView1.Columns["BEGIN_TIME"].HeaderText = "开始时间";
                dataGridView1.Columns["END_TIME"].HeaderText = "结束时间";
                dataGridView1.Columns["HIRE_STATE"].HeaderText = "租用状态";
                dataGridView1.Columns["ROWNUMBER"].Visible = false;
                dataGridView1.RowHeadersVisible = false;
                dataGridView1.AllowUserToAddRows = false;
                dataGridView1.AllowUserToResizeColumns = false;
                dataGridView1.AllowUserToResizeRows = false;
                dataGridView1.ReadOnly = true;
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                foreach (DataGridViewColumn column in dataGridView1.Columns)
                {
                    column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    column.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                }
            }


        }
        public void CardID_Set(string cardID)
        {
            Card_ID = cardID;
        }
        public void branch_and_staff_get(string branchID, string staffID)
        {
            branch_id = branchID;
            staff_id = staffID;
        }
        //返回
        private void button8_Click(object sender, EventArgs e)
        {
            ButtonBack?.Invoke(this, EventArgs.Empty);
        }
        //重置
        private void button2_Click(object sender, EventArgs e)
        {
            page = 1;
            totalRecordCount = Safe_Box_Operations.
                GetSafeBoxRecordCount(Card_ID, dateTimePicker1.Text, dateTimePicker2.Text);
            // 将返回的 DataTable 绑定到 dataGridView1
            dataGridView1.DataSource =
                Safe_Box_Operations.GetSafeBoxRecords(page.ToString(), dateTimePicker1.Text, dateTimePicker2.Text, Card_ID);
            // 设置每一列
            label9.Text = "当前第  " + $"{page}" + "  页        共有  " + $"{(totalRecordCount - 1) / 10 + 1}  页 ";
            SetDataGridViewColumnWidthsAndRowHeights();
        }
        //筛选
        private void button1_Click(object sender, EventArgs e)
        {
            totalRecordCount = Safe_Box_Operations.GetSafeBoxRecordCount(Card_ID, dateTimePicker1.Text, dateTimePicker2.Text);
            page = 1;
            // 将返回的 DataTable 绑定到 dataGridView1
            dataGridView1.DataSource =
                Safe_Box_Operations.GetSafeBoxRecords(page.ToString(), dateTimePicker1.Text, dateTimePicker2.Text, Card_ID);
            // 设置每一列
            label9.Text = "当前第  " + $"{page}" + "  页        共有  " + $"{(totalRecordCount - 1) / 10 + 1}  页 ";
            SetDataGridViewColumnWidthsAndRowHeights();
        }

        private void button12_Click(object sender, EventArgs e)
        {
            //上一页
            if (page == 1)
            {
                return;
            }
            else
            {
                page--;
                dataGridView1.DataSource =
                Safe_Box_Operations.GetSafeBoxRecords(page.ToString(), dateTimePicker1.Text, dateTimePicker2.Text, Card_ID);
                // 设置每一列
                label9.Text = "当前第  " + $"{page}" + "  页        共有  " + $"{(totalRecordCount - 1) / 10 + 1}  页 ";
                SetDataGridViewColumnWidthsAndRowHeights();
            }
        }
        private void button13_Click(object sender, EventArgs e)
        {
            //下一页
            if (page == (totalRecordCount - 1) / 10 + 1)
            {
                return;
            }
            else
            {
                page++;
                dataGridView1.DataSource =
                Safe_Box_Operations.GetSafeBoxRecords(page.ToString(), dateTimePicker1.Text, dateTimePicker2.Text, Card_ID);
                // 设置每一列
                label9.Text = "当前第  " + $"{page}" + "  页        共有  " + $"{(totalRecordCount - 1) / 10 + 1}  页 ";
                SetDataGridViewColumnWidthsAndRowHeights();
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {

        }
    }
}
